package com.ruoyi.system.service.module.estate.impl;

import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.exception.TransactionalException;
import com.ruoyi.system.domain.module.estate.Estate;
import com.ruoyi.system.domain.module.estate.EstateRoom;
import com.ruoyi.system.domain.module.estate.PaySlip;
import com.ruoyi.system.domain.module.excel.PropertyWyfTemplateExcel;
import com.ruoyi.system.domain.module.property.PropertyFeeOrderTemplate;
import com.ruoyi.system.mapper.module.estate.EstateRoomMapper;
import com.ruoyi.system.mapper.module.estate.PaySlipMapper;
import com.ruoyi.system.service.module.estate.IPaySlipService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;


/**
 * 工资条Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-24
 */
@Service
public class PaySlipServiceImpl extends ServiceImpl<PaySlipMapper, PaySlip> implements IPaySlipService {
	@Resource
	private PaySlipMapper paySlipMapper;
	@Override
	public String importWyfData(List<PaySlip> templateList, Boolean isUpdateSupport, String operName) {
		int ret = 0;
		int successNum = 0;
		int failureNum = 0;
		StringBuilder successMsg = new StringBuilder();
		StringBuilder failureMsg = new StringBuilder();
		for (PaySlip slip : templateList) {
			try {
				slip.setState(0);
				ret =paySlipMapper.insert(slip);
				if (ret <= 0) {
					throw new TransactionalException("orderTemplateMapper insert error");
				}
				successNum++;
			} catch (Exception e) {
				failureNum++;
				String msg = "<br/>" + failureNum + "、姓名 " + slip.getRealName() + "-" + slip.getYearNum()+"年"+slip.getMonthNum()+"月工资记录导入失败：";
				failureMsg.append(msg + e.getMessage());
				log.error(msg, e);
			}
		}
		if (failureNum > 0) {
			failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
			throw new CustomException(failureMsg.toString());
		} else {
			successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
		}
		return successMsg.toString();
	}
}
